//
//  ZHWindowButtonView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/6.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class ZHWindowButtonView: NSView {
    
    var mouseInside:Bool = false
    let kBtnX:CGFloat = 16
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.commonInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func commonInitialize() {
        self.addSubview(closeButton)
        self.addSubview(minButton)
        self.addSubview(maxButton)
        self.addNotifications()
    }
    
    func addNotifications() {
        let defaultCenter = NotificationCenter.default
        defaultCenter.addObserver(self, selector: #selector(applicationWillBecomeActive(_:)), name: NSApplication.willBecomeActiveNotification, object: NSApp)
        defaultCenter.addObserver(self, selector: #selector(applicationDidResignActive(_:)), name: NSApplication.didResignActiveNotification, object: NSApp)
        defaultCenter.addObserver(self, selector: #selector(windowActiveChanged(_:)), name: NSApplication.didBecomeActiveNotification, object: nil)
    }
   
    lazy var closeButton: NSButton = {
        let btn = NSWindow.standardWindowButton(NSWindow.ButtonType.closeButton, for: NSWindow.StyleMask(rawValue: 3))!
        btn.frame = NSMakeRect(10, 0, btn.bounds.size.width, btn.bounds.size.height)
        return btn
    }()
    lazy var minButton: NSButton = {
        let btn =  NSWindow.standardWindowButton(NSWindow.ButtonType.miniaturizeButton, for: NSWindow.StyleMask(rawValue: 3))!
        btn.frame = NSMakeRect(30, 0, btn.bounds.size.width, btn.bounds.size.height)
        return btn
    }()
    lazy var maxButton: NSButton = {
        let btn =  NSWindow.standardWindowButton(NSWindow.ButtonType.zoomButton, for: NSWindow.StyleMask(rawValue: 3))!
        btn.frame = NSMakeRect(50, 0, btn.bounds.size.width, btn.bounds.size.height)
        return btn
    }()
    
    func setNeedsDisplayForStandardWindowButtons()  {
        self.closeButton.setNeedsDisplay()
        self.minButton.setNeedsDisplay()
        self.maxButton.setNeedsDisplay()
    }

    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        let area = NSTrackingArea.init(rect: NSRect.zero, options: [NSTrackingArea.Options.mouseEnteredAndExited,NSTrackingArea.Options.activeAlways,NSTrackingArea.Options.inVisibleRect], owner: self, userInfo: nil)
        self.addTrackingArea(area)
    }
    
    @objc func applicationWillBecomeActive(_ notification:Notification) {
        
        self.mouseInside = true
        self.setNeedsDisplayForStandardWindowButtons()
        
        self.mouseInside = false
        self.setNeedsDisplayForStandardWindowButtons()
    }
    @objc func applicationDidResignActive(_ notification:Notification) {
        self.mouseInside = false
        self.setNeedsDisplayForStandardWindowButtons()
    }
    @objc func windowActiveChanged(_ notification:Notification) {
        self.mouseInside = false
        self.setNeedsDisplayForStandardWindowButtons()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        self.mouseInside = true
        self.setNeedsDisplayForStandardWindowButtons()
    }
    
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        self.mouseInside = false
        self.setNeedsDisplayForStandardWindowButtons()
    }
    func _mouseInGroup(_ button:NSButton) -> Bool {
        return self.mouseInside
    }
    
    func resetButtonPosition() {
        if self.maxButton.isHidden {
            if self.minButton.isHidden {
                
            }else {
                if self.closeButton.isHidden {
                    self.minButton.frame = NSRect.init(x: 10, y: 0, width: kBtnX, height: kBtnX)
                }else {
                    self.minButton.frame = NSRect.init(x: 30, y: 0, width: kBtnX, height: kBtnX)
                }
            }
        }else {
            if self.minButton.isHidden {
                if self.closeButton.isHidden {
                    self.maxButton.frame = NSRect.init(x: 10, y: 0, width: kBtnX, height: kBtnX)
                }else {
                    self.maxButton.frame = NSRect.init(x: 30, y: 0, width: kBtnX, height: kBtnX)
                }
            }else {
                if self.closeButton.isHidden {
                    self.minButton.frame = NSRect.init(x: 10, y: 0, width: kBtnX, height: kBtnX)
                    self.maxButton.frame = NSRect.init(x: 30, y: 0, width: kBtnX, height: kBtnX)
                }else {
                    self.closeButton.frame = NSRect.init(x: 10, y: 0, width: kBtnX, height: kBtnX)
                    self.minButton.frame = NSRect.init(x: 30, y: 0, width: kBtnX, height: kBtnX)
                    self.maxButton.frame = NSRect.init(x: 50, y: 0, width: kBtnX, height: kBtnX)
                }
            }
        }
    }
}
